<template>
	<view class="goodsdetail" v-if="goodsinfo">
		<view class="banner">
			<swiper class="uni-swiper" :indicator-dots="true" circular="true" :autoplay="true" :interval="3000"
				:duration="1000">
				<swiper-item v-for="(item,index) in goodsinfo.image" :key='index'>
					<view class="swiper-item">
						<image :src="item.path" mode=""></image>
					</view>
				</swiper-item>

			</swiper>
		</view>
		<view class="goodsinfo">
			<view class="price">
				<view class="pricenum">￥<text>{{goodsinfo.price}}</text></view>
				<view class="cate">
					<image :src="goodsinfo.cate_path" mode=""></image>
					{{goodsinfo.cate_name}}
				</view>
			</view>
			<view class="name">
				{{goodsinfo.name}}
			</view>
			<view class="specs">
				<view class="content">
					{{goodsinfo.sketch}}
				</view>
				<view class="num">
					已售：{{goodsinfo.sales}}
				</view>
			</view>
		</view>
		<view class="detail">
			<view class="title">
				产品详情
			</view>
			<rich-text class="msg" :nodes="getContent(goodsinfo.intro)"></rich-text>
		</view>
		<view class="footer">
			<button class="left" open-type="contact">
				<uni-icons type="headphones" color="#FFD256" size="20"></uni-icons>
				<text class="l-text">联系客服</text>
			</button>
			<view class="right">
				<view class="btns">
					<!-- <view class="joincart" @click="joinCart">
						加入购物车
					</view> -->
					<view class="buynow"
						@click="is_login?toPage('/pages/orderfound/index?goods_id='+goodsinfo.id):showLogin()">
						立即下单
					</view>
				</view>
			</view>
		</view>
		<y-login :show_login="show_login" :refer_id="refer_id" @closeLogin="closeLogin" @getUser="getUser"></y-login>
	</view>
</template>

<script>
	import {
		GoodsDetail,
		getCartSum
	} from "@/api/api.js"
	import {
		add
	} from "@/api/shop.js"
	export default {
		data() {
			return {
				goodsid: '',
				goodsinfo: '',
				refer_id: '',
				user_id: '',
				num: '',
				show_login: false,
				is_login: false
			}
		},
		onLoad(e) {
			if (e.id) {
				this.goodsid = e.id
				// this.getCartSum()
			}
			if (e.refer_id) this.refer_id = e.refer_id

		},
		onShow() {
			this.show_login = false
			this.GoodsDetail()
		},
		onShareAppMessage() {
			return {
				title: this.goodsinfo.name,
				path: `/pages/goodsdetail/index?id=${this.goodsid}&refer_id=${this.user_id}`
			}
		},
		onShareTimeline() {

		},
		methods: {
			getUser() {
				uni.navigateTo({
					url: '/pages/orderfound/index?goods_id=' + this.goodsinfo.id
				})
			},
			closeLogin(e) {
				this.show_login = e
			},
			showLogin() {
				this.show_login = true
			},
			getContent(content) {
				let url = this.$config.websiteUrl
				let style = `style="max-width:100%;width:100%;display:block;"`

				if (typeof(content) == 'string') {
					content = content.replace(/src="\/ueditor/g, `src="${url}/ueditor`)
					return content.replace(/<(img).*?(\/>|<\/img>)/g, function(mats) {
						if (mats.indexOf('style') < 0) {
							return mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
						} else {
							return mats.replace(/style=("|')/, 'style=$1max-width:100%;height:auto;')
						}
					});
					return
					content.replace(/style=""/g, ``)
					return content.replace(/src="\/ueditor/g, `${style} src="${url}/ueditor`)
				}
			},
			toHome() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			joinCart() {
				add({
					goods_id: this.goodsid,
					num: 1
				}).then(res => {
					uni.$showMsg(res.message)
					if (res.code == 0) {
						this.getCartSum()
					}
				})
			},
			getCartSum() {
				getCartSum().then(res => {
					if (res.code == 0) {
						this.num = res.data
					}
				})
			},
			toCart() {
				uni.switchTab({
					url: '/pages/cart/index'
				})
			},
			GoodsDetail() {
				GoodsDetail({
					id: this.goodsid,
					refer_id: this.refer_id
				}).then(res => {
					if (res.code == 0) {
						this.goodsinfo = res.data
						this.is_login = this.goodsinfo.is_login
						this.user_id = this.goodsinfo.user_id || ''
						if (this.goodsinfo.refer_tips) {
							//谁推荐了此商品
							uni.$showMsg(this.goodsinfo.refer_tips)
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.goodsdetail {
		height: 100%;
		min-height: 100vh;
		/* #ifdef H5 */
		min-height: calc(100vh - 50px);
		/* #endif */
		box-sizing: border-box;
		position: relative;
		background-color: #000;
		max-width: 500px;
		padding-bottom: 40px;
	}

	.uni-swiper {
		height: 350px;

		.swiper-item {
			image {
				width: 100%;
				height: 350px;
			}
		}
	}

	.goodsinfo {
		background: #212327;
		padding: 20px 25rpx;

		.price {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.pricenum {
				color: #FFD256;

				text {
					font-weight: bold;
					font-size: 24px;
				}
			}

			.cate {
				display: flex;
				align-items: center;
				font-size: 14px;
				color: #FFD256;
				background: rgba(143, 124, 91, 0.2);
				border-radius: 20px;
				padding: 4px 10px;

				image {
					width: 20px;
					height: 20px;
					margin-right: 8px;
					border-radius: 50%;
				}
			}
		}

		.name {
			letter-spacing: 1px;
			font-weight: bold;
			font-size: 17px;
			color: #fff;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
			margin-top: 10px;
		}

		.specs {
			display: flex;
			justify-content: space-between;
			margin-top: 10px;
			color: #8C8C8C;

			.num {
				font-size: 12px;
				width: 80px;
				text-align: right;
			}

			.content {
				font-size: 14px;

				width: calc(100% - 80px);
				display: -webkit-box;
				-webkit-line-clamp: 1;
				-webkit-box-orient: vertical;
				overflow: hidden;
			}
		}
	}

	.detail {
		background-color: transparent;
		margin-top: 20rpx;
		min-height: 200rpx;
		padding: 0 10px;
		padding-bottom: 130rpx;

		.title {
			text-align: center;
			padding: 30rpx;
			color: #777;
		}

		image {
			width: 92%;
			margin-left: 4%;
		}

		.msg {
			box-sizing: border-box;
			color: #fff;
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		width: 100%;
		display: flex;
		background: #fff;
		max-width: 500px;
		background: #212327;
		height: 50px;
		align-items: center;

		.left {
			width: 70px;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			color: #FFD256;
			background: transparent;
			margin: 0;
			padding: 0;
			height: 50px;
			line-height: normal;
			font-size: 12px;

			&:before {
				border: 0;
			}

			.l-text {
				margin-top: 2px;
			}

			text {
				// background: #f00;
				// color: #fff;
				// border-radius: 50%;
				// width: 15px;
				// height: 15px;
				// display: block;
				// text-align: center;
				// line-height: 15px;
				// font-size: 12px;
				// font-weight: bold;
				// margin: 10px 0 0 -10px;
				// position: relative;
				// z-index: 1001;
			}

			image {
				width: 25px;
				height: 25px;
			}
		}

		.right {
			flex: 1;
			display: flex;
			color: #fff;
			text-align: center;
			justify-content: flex-end;
			padding-right: 10px;
			box-sizing: border-box;
			height: 100rpx;

			.btns {
				display: flex;
				width: 96%;
				border-radius: 20px;
				height: 80rpx;
				line-height: 80rpx;
				margin-top: 10rpx;
				overflow: hidden;
			}

			.joincart {
				width: 50%;
				background: #FFB049;
			}

			.buynow {
				width: 100%;
				font-size: 16px;
				background: linear-gradient(90deg, #E3C786 0%, #CBA443 100%);
				color: #000;
				font-weight: bold;
			}
		}
	}
</style>